<template>
    <orh-table reload  @on-reload="reload" pages :pageDate="pageDate" @on-page-change="queryData" :hideTitle="hideTitle">
        <Table border stripe :columns="columns" :data="data" :loading="tableload" slot="table"></Table>
    </orh-table>
</template>
                
<script>
export default {
    props: {
        hideTitle: {
            type: Boolean,
            default: true
        }
    },
    components: {
    },
    data () {
        return {
            tableload:false,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            data:[],
            columns: [],
            url:'api/watchlist/v1/watchlistfrostatistics/useridsindexcont?page=',
        }
    },
    methods: {

        // ==================================================
        // 手动刷新
        // ==================================================
        reload(){
            this.data = []
            this.columns = []
            this.tableload = true
            this.queryData(this.pageDate.current_page)
        },

        // ==================================================
        // 请求数据
        // ==================================================
        queryData(index){
            this.$http.get(this.url + index).then(val=>{
                    this.handelData(val.data[0])
                    this.pageDate.per_page = val.pages.per_page
                    this.pageDate.total = val.pages.total
                    this.pageDate.current_page = val.pages.current_page
                    this.data = val.data
                    this.tableload = false
                
                console.log(val)
            })
            .catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.tableload = false  
            })
        },

        // ==================================================
        // 数据处理
        // ==================================================
        handelData(datas){
            this.columns.push({
                title: '姓名',
                align: 'center',
                minWidth: 150,
                render:(h,params)=> {
                        return h('span',params.row.teacher_name?params.row.teacher_name.zh?params.row.teacher_name.zh:params.row.teacher_name.mn:'')
                    }
            })
            datas.data.forEach(el => {
                this.columns.push({
                    title: el.watchlist_name.zh?el.watchlist_name.zh:el.watchlist_name.mn,
                    align: 'center',
                    minWidth: 150,
                    value: el.watchlist_id,
                    render: (h, params) => {
                        let val = params.row.data.find(i => i.watchlist_id == params.column.value)
                        return h('div', val.watchlistcont)
                    }
                })
            });
            
            this.columns.push({
                title: '总次数',
                align: 'center',
                minWidth: 100,
                render: (h, params) => {
                    let val = params.row.data[0].cont
                    return h('div', val)
                }
            })
        },
    },
    mounted(){
        this.queryData(1)
    },
}
</script>